<extend name="./Application/Admin/View/common/base.html" />
<block name="crumb">
    <span class="c-gray en">&gt;</span> 客户系统
    <span class="c-gray en">&gt;</span> 客户列表
    <span class="c-gray en">&gt;</span> 客户转化
</block>
<block name="content">
    <div class="tabBar cl m-b-10">
        <span class="current"><a href="{:U('Customer/index')}">客户列表</a></span>
    </div>

    <form class="form form-horizontal col-xs-6" id="to_agent" method="post" action="{:U('Customer/to_agent')}">
        <div class="row cl">
            <label class="form-label col-xs-3 col-sm-3"><span class="c-red">*</span>代理级别：</label>
            <div class="formControls col-xs-3 col-sm-3">
                <select name="rank_id" id="grade" class="input-text">
                    <foreach name="ranks" item="rank">
                        <option value="{$rank['rank_id']}">{$rank['rank_name']}</option>
                    </foreach>
                </select>
            </div>
            <div class="formControls col-xs-3 col-sm-3">
                <a href="{:U('Agent/add_rank')}" style="color:#1273A3;text-decoration:underline;">去添加</a>
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-3 col-sm-3"><span class="c-red">*</span>代理姓名：</label>
            <div class="formControls col-xs-7 col-sm-7">
                <input type="text" class="input-text" value="{$customer['c_name']}" disabled/>
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-3 col-sm-3"><span class="c-red">*</span>手机号：</label>
            <div class="formControls col-xs-3 col-sm-3">
                <input type="text" class="input-text" value="{$customer['mobile']}" disabled/>
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-3 col-sm-3">微信号：</label>
            <div class="formControls col-xs-7 col-sm-7">
                <input type="text" class="input-text" value="{$customer['wx_num']}" disabled/>
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-3 col-sm-3"><span class="c-red">*</span>登陆用户名：</label>
            <div class="formControls col-xs-3 col-sm-3">
                <input type="text" class="input-text" id="username" name="username" onchange="checkIsset('n', this.value);"/>
            </div>
            <div class="formControls col-xs-4 col-sm-4" id="uname_tip"></div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-3 col-sm-3"><span class="c-red">*</span>登陆密码：</label>
            <div class="formControls col-xs-7 col-sm-7">
                <input type="text" class="input-text" id="pwd" name="pwd"/>
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>性别：</label>
            <div class="formControls col-xs-8 col-sm-9 skin-minimal">
                <label><input name="sex" type="radio" checked value="0"/> 保密</label>
                <label><input name="sex" type="radio" value="1"/> 男</label>
                <label><input name="sex" type="radio" value="2"/> 女</label>
            </div>
        </div>
        <div class="row cl" style="text-align: center;">
            <label class="form-label col-xs-12 col-sm-12"></label>
            <input type="hidden" name="c_id" value="{$customer['c_id']}"/>
            <button id="save" class="btn btn-secondary radius" type="button">添加代理</button>
        </div>
    </form>
</block>

<block name="js">
<script type="text/javascript">
    $(function(){
        $('#save').click(function () {
            var grade = $('#grade').val();
            var c_name = $('#c_name').val();
            var mobile = $('#mobile').val();
            var username = $('#username').val();
            var pwd = $('#pwd').val();

            if (grade == '') {
                layer.alert('代理层级必选！');
                return false;
            }
            if (c_name == '') {
                layer.alert('代理名称必填！');
                return false;
            }
            if (mobile == '') {
                layer.alert('代理手机必填！');
                return false;
            }
            if (username == '' || username.length < 3) {
                layer.alert('用户名必填，且是不能少于3位的纯字母！');
                return false;
            }
            if (pwd == '') {
                layer.alert('登陆密码必填！');
                return false;
            }
            $("#to_agent").submit();
        });

        $("#search_to").keyup(function(){
            $aName = $(this).val();
            if($aName != ''){
                getAgentName();
            }else{
                return false;
            }
        });

        $("#search_to").change(function(){
            $aName = $(this).val();
            if($aName != ''){
                getAgentName();
            }else{
                return false;
            }
        });

        function getAgentName()
        {
            $.ajax({
                url: "{:U('Members/searchAgent')}",
                type: "post",
                data: {aName : $aName},
                success: function (data)
                {
                    options = '<option value="0">-- 顶级代理 --</option>';
                    if (data) {
                        $.each(data, function(k, v){
                            options += '<option value="'+ v.agent_id +'">'+ v.agent_name +'</option>';
                        });
                    }
                    $("#pid").html(options);
                }
            });
        }
    });
    function checkIsset($type, $searchKey)
    {
        $.ajax({
            url: "{:U('Agent/searchIsset')}",
            type: "post",
            data: {skey : $searchKey ,field :$type},
            dataType:'JSON',
            success: function (msg)
            {
                console.log(msg);
                if(msg.status == 'success'){
                    if($type == 'n'){
                        $("#uname_tip").html("<span class='label label-success'>可用</span>");
                    }else{
                        $("#mobile_tip").html("<span class='label label-success'>可用</span>");
                    }
                }else{
                    if($type == 'n'){
                        $("#uname_tip").html("<span class='label label-danger'>"+ msg.msg +"</span>");
                    }else{
                        $("#mobile_tip").html("<span class='label label-danger'>"+ msg.msg +"</span>");
                    }
                }
            }
        });
    }
</script>
</block>